package com.sugon.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.Follow8dRecordDao;
import com.sugon.entity.Follow8dRecordEntity;
import com.sugon.entity.SysMsgEntity;
import com.sugon.service.Follow8dRecordService;
import com.sugon.service.SysMsgService;
import com.sugon.service.SysProcessInfoService;
import com.sugon.utils.Constant;
import com.sugon.utils.ProcessInfoEnum;
import com.sugon.utils.ShiroUtils;
import com.sugon.utils.SysMsgType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 8D跟踪反馈记录Service实现类
 *
 * @author sugon
 * @date 2019-08-14 15:46:07
 */
@Service("follow8dRecordService")
public class Follow8dRecordServiceImpl implements Follow8dRecordService {
    @Autowired
    private Follow8dRecordDao follow8dRecordDao;
    @Autowired
    private SysProcessInfoService sysProcessInfoService;
    @Autowired
    private SysMsgService sysMsgService;
    @Override
    public Follow8dRecordEntity queryObject(String id) {
        return follow8dRecordDao.queryObject(id);
    }

    @Override
    public List<Follow8dRecordEntity> queryList(Map<String, Object> map) {
        return follow8dRecordDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return follow8dRecordDao.queryTotal(map);
    }

    @Override
    public int save(Follow8dRecordEntity follow8dRecord) {
        follow8dRecord.setId(RandomUtil.randomString(32));
        follow8dRecord.setCreateUser(getUserId());
        follow8dRecord.setCreateDate(new Date());
        /**
         * 确认完成效果 默认未确定
         */
        follow8dRecord.setRecheckStatus(Integer.valueOf(Constant.IS_NOT_ADOPT));
        /**
         * 审核完成结果 默认未审核
         */
        follow8dRecord.setReviewStatus(Integer.valueOf(Constant.IS_NOT_ADOPT));
        follow8dRecord.setDelFlag(Integer.valueOf(Constant.IS_NOT_DEL));
        //添加审核 消息提醒
        sysProcessInfoService.saveProcessInfo(follow8dRecord.getId(), ProcessInfoEnum.DEV_EDSP, 0, null);
        SysMsgEntity sysMsgEntity = new SysMsgEntity();
        String id = follow8dRecord.getId();
        sysMsgEntity.setDataId(id);
        sysMsgEntity.setDataUrl("../device/follow8drecord.html?id=" + id);
        sysMsgService.addApproveItem(ProcessInfoEnum.DEV_EDSP,sysMsgEntity,0);
        return follow8dRecordDao.save(follow8dRecord);
    }

    @Override
    public int update(Follow8dRecordEntity follow8dRecord) {
        follow8dRecord.setUpdateDate(new Date());
        follow8dRecord.setUpdateUser(getUserId());
        int rows = follow8dRecordDao.update(follow8dRecord);
        if (rows > 0) {
            SysMsgEntity sysMsgEntity = new SysMsgEntity();
            String id = follow8dRecord.getId();
            sysMsgEntity.setDataId(id);
            sysMsgEntity.setDataUrl("../device/follow8drecord.html?id=" + id);
            sysMsgService.addApproveItem(ProcessInfoEnum.DEV_EDSP, sysMsgEntity, 0);
        }
        return rows;
    }

    @Override
    public int delete(String id) {
        return follow8dRecordDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return follow8dRecordDao.deleteBatch(ids);
    }

    @Override
    public int review(Follow8dRecordEntity follow8dRecord) {
        follow8dRecord.setReviewer(getUserId());
        follow8dRecord.setReviewTime(new Date());
        sysProcessInfoService.saveProcessInfo(follow8dRecord.getId(), ProcessInfoEnum.DEV_EDSP,  0,follow8dRecord.getReviewer()  ,null);
        SysMsgEntity sysMsgEntity = new SysMsgEntity();
        String Id = follow8dRecord.getId();
        sysMsgEntity.setDataId(Id);
        sysMsgEntity.setDataType(ProcessInfoEnum.DEV_EDSP);
        sysMsgEntity.setDataFlag(SysMsgType.DATA_DEL_END);
        sysMsgService.allMsgData(sysMsgEntity);

        sysProcessInfoService.saveProcessInfo(follow8dRecord.getId(), ProcessInfoEnum.DEV_RECHECK, 0, null);
        SysMsgEntity sysMsgEntity2 = new SysMsgEntity();
        String id = follow8dRecord.getId();
        sysMsgEntity2.setDataId(id);
        sysMsgEntity2.setDataUrl("../device/follow8drecord.html?id=" + id);
        sysMsgService.addApproveItem(ProcessInfoEnum.DEV_RECHECK, sysMsgEntity2, 0);
        return follow8dRecordDao.update(follow8dRecord);
    }

    @Override
    public int recheck(Follow8dRecordEntity follow8dRecord) {
        follow8dRecord.setRecheckUser(getUserId());
        follow8dRecord.setRecheckTime(new Date());
        follow8dRecord.setRelCloseDate(new Date());
        sysProcessInfoService.saveProcessInfo(follow8dRecord.getId(), ProcessInfoEnum.DEV_RECHECK, 0,follow8dRecord.getRecheckUser(),null);

        int rows = follow8dRecordDao.update(follow8dRecord);
        if (rows > 0){
            SysMsgEntity sysMsgEntity = new SysMsgEntity();
            String Id = follow8dRecord.getId();
            sysMsgEntity.setDataId(Id);
            sysMsgEntity.setDataType(ProcessInfoEnum.DEV_RECHECK);
            sysMsgEntity.setDataFlag(SysMsgType.DATA_DEL_END);
            sysMsgService.allMsgData(sysMsgEntity);
        }
        return rows;
    }

    @Override
    public int saveDev8D(Follow8dRecordEntity follow8dRecord) {
        follow8dRecord.getWorkId();
        follow8dRecord.setId(RandomUtil.randomString(32));
        follow8dRecord.setCreateUser(getUserId());
        follow8dRecord.setCreateDate(new Date());
        /**
         * 确认完成效果 默认未确定
         */
        follow8dRecord.setRecheckStatus(Integer.valueOf(Constant.IS_NOT_ADOPT));
        /**
         * 审核完成结果 默认未审核
         */
        follow8dRecord.setReviewStatus(Integer.valueOf(Constant.IS_NOT_ADOPT));
        follow8dRecord.setDelFlag(Integer.valueOf(Constant.IS_NOT_DEL));
        //添加审核 消息提醒
        sysProcessInfoService.saveProcessInfo(follow8dRecord.getId(), ProcessInfoEnum.DEV_EDSP, 0, null);
        SysMsgEntity sysMsgEntity = new SysMsgEntity();
        sysMsgEntity.setDataId(follow8dRecord.getId());
        sysMsgEntity.setDataUrl("../device/follow8drecord.html?id=" + follow8dRecord.getId());
        sysMsgEntity.setType(SysMsgType.APPROVAL_REMIND);
        sysMsgEntity.setUserId(ShiroUtils.getUserId());
        sysMsgEntity.setDept(ShiroUtils.getDeptId());
        sysMsgEntity.setTitle(ProcessInfoEnum.DEV_EDSP.getDesc());
        sysMsgEntity.setDataType(ProcessInfoEnum.DEV_EDSP);
        sysMsgService.allMsgData(sysMsgEntity);
        return follow8dRecordDao.save(follow8dRecord);
    }
}
